package com.aaa.utils;

import cn.hutool.core.collection.ListUtil;

import java.util.List;
import java.util.ArrayList;

public class AuthPathMatcherUtil {

    public static boolean matches(String pattern, String path) {
        if (pattern == null || path == null) {
            return false;
        }

        String[] patternParts = trimSlashes(pattern).split("/");
        String[] pathParts = trimSlashes(path).split("/");

        if (pattern.contains("**")) {
            return matchDoubleWildcard(patternParts, pathParts);
        } else {
            return matchSingleWildcard(patternParts, pathParts);
        }
    }

    private static String trimSlashes(String path) {
        return path.replaceAll("^/+", "").replaceAll("/+$", "");
    }

    private static boolean matchSingleWildcard(String[] patternParts, String[] pathParts) {
        if (patternParts.length != pathParts.length) {
            return false;
        }

        for (int i = 0; i < patternParts.length; i++) {
            String p = patternParts[i];
            if (!p.equals("*") && !p.equals(pathParts[i])) {
                return false;
            }
        }

        return true;
    }

    private static boolean matchDoubleWildcard(String[] patternParts, String[] pathParts) {
        List<String> patternList = new ArrayList<>();
        for (String part : patternParts) {
            patternList.add(part);
        }

        int pIndex = 0;
        int pathIndex = 0;

        while (pIndex < patternList.size() && pathIndex < pathParts.length) {
            String currentPattern = patternList.get(pIndex);

            if ("**".equals(currentPattern)) {
                if (pIndex == patternList.size() - 1) {
                    return true;
                }

                String nextPattern = patternList.get(pIndex + 1);
                boolean matched = false;
                while (pathIndex < pathParts.length) {
                    if (nextPattern.equals(pathParts[pathIndex])) {
                        matched = true;
                        pathIndex++;
                        break;
                    }
                    pathIndex++;
                }

                if (!matched) {
                    return false;
                }

                pIndex += 2;
            } else {
                if (!currentPattern.equals(pathParts[pathIndex])) {
                    return false;
                }
                pIndex++;
                pathIndex++;
            }
        }

        return pIndex >= patternList.size() && pathIndex >= pathParts.length;
    }

    /**
     * 判断路径是否匹配任意一个 pattern
     */
    public static boolean matchesAny(List<String> patterns, String path) {
        if (patterns == null || patterns.isEmpty() || path == null) {
            return false;
        }

        for (String pattern : patterns) {
            if (matches(pattern, path)) {
                return true;
            }
        }

        return false;
    }

    // 测试用例
    public static void main(String[] args) {
        List<String> patterns = ListUtil.of("/api/**", "/user/*", "/login");

        System.out.println(matchesAny(patterns, "/api/test"));         // true
        System.out.println(matchesAny(patterns, "/user/123"));         // true
        System.out.println(matchesAny(patterns, "/login"));            // true
        System.out.println(matchesAny(patterns, "/admin/dashboard"));  // false
    }
}